$(function(){
    $('#exchangetype').change(function(){
        $('#fnum').val('');
        $('#tnum').val('');
        switch ($(this).val()){
            case 'wtc':
                $('#fnum').next('span').text('微币');
                $('#tnum').next('span').text('积分');
                break;
            case 'ctw':
                $('#fnum').next('span').text('积分');
                $('#tnum').next('span').text('微币');
                break;
        }
    });
    $('#fnum').keyup(function(){
        var fnum = $(this).val();
        var rate = parseInt($('#wbrate').text());
        if(fnum){
            switch ($('#exchangetype').val()){
                case 'wtc':
                    $('#tnum').val(parseInt(fnum)/rate);
                    break;
                case 'ctw':
                    $('#tnum').val(parseInt(fnum)*rate);
                    break;
            }
        }
    });
    $('#submit').click(function(){
        var fmax = 0;
        var type = '';
        switch ($('#exchangetype').val()){
            case 'wtc':
                fmax = parseFloat($('#wbspan').text());
                type = '微币';
                break;
            case 'ctw':
                fmax = parseFloat($('#scspan').text());
                type = '积分';
                break;
        }
        if($('#fnum').val()!=undefined && $('#fnum').val()==""){
            $.Dialog.fail("输入错误！");
        }
        else{
            if(parseFloat($('#fnum').val()) > fmax){
                $.Dialog.fail("您的" + type + "不够噢.");
            }
            else{
                $.ajax({
                    url: wbexchange.wbtoexchange,
                    type:"post",
                    data:{exchangetype: $('#exchangetype').val(), fnum: parseFloat($('#fnum').val())},
                    dataType:"json",
                    success:function(data){
                        if(data.statu){
                            $.Dialog.success(data.content);
                            $('#wbspan').text(data.wb);
                            $('#scspan').text(data.score);
                        }
                        else{
                            $.Dialog.fail(data.content);
                        }
                    }
                });
            }
        }
    })
})